SRC_FILES := src/exploit.c src/env.c src/net.c src/nftnl.c src/file.c
OUT_NAME = ./exploit

# use musl-gcc since statically linking glibc with gcc generated invalid opcodes for qemu
#   and dynamically linking raised glibc ABI versioning errors
CC = musl-gcc

CFLAGS = -I./include -Wall -Wno-deprecated-declarations

# use custom object archives compiled with musl-gcc for compatibility. normal ones 
#   are used with gcc and have _chk funcs which musl doesn't support
# - ./include/libmnl: libmnl v1.0.5
# - ./include/libnftnl: libnftnl v1.2.6
LIBMNL_PATH = ./lib/libmnl.a
LIBNFTNL_PATH = ./lib/libnftnl.a

exploit: _compile_static _strip_bin
prerequisites: _install_musl _install_headers
run: _run_outfile
clean: _clean_outfile

_install_headers:
	sudo apt-get install libmnl-dev libnftnl-dev

	# incredibly cursed way to manage musl-gcc include paths (by doing -I/usr/include I got errors like <bits/wordsize.h> not being found)
	mkdir include
	ln -s /usr/include/libnftnl ./include/libnftnl
	ln -s /usr/include/libmnl ./include/libmnl
	ln -s /usr/include/linux ./include/linux
	ln -s /usr/include/x86_64-linux-gnu/asm ./include/asm
	ln -s /usr/include/asm-generic ./include/asm-generic

_install_musl:
	sudo apt-get install musl-tools
_compile_static:
	$(CC) $(CFLAGS) $(SRC_FILES) -o $(OUT_NAME) -static $(LIBNFTNL_PATH) $(LIBMNL_PATH)
_strip_bin:
	strip $(OUT_NAME)
_run_outfile:
	$(OUT_NAME)
_clean_outfile:
	rm $(OUT_NAME)